using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Reporting.WebForms;
using MvcExtExample.Models;

namespace MvcExtExample.ReportViews
{
    public partial class ZSJReportView : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["LogedUser"] == null)
                Response.Redirect("http://localhost:8132/home/index");
            if (!IsPostBack)
            {
                //获取所有工作区的名称
                this.WorkZoneDropDownList.DataSource = GetWorkZoneData();
                this.WorkZoneDropDownList.DataBind();
                ////获取“第一作业区”下的注水大队列表
                this.SCDDDropDownList.DataSource = GetZSDDData("第一作业区");
                this.SCDDDropDownList.DataBind();
                ////获取“第一注水大队”下的生产小队列表
                this.SCXDDropDownList.DataSource = GetZSDDData("第一注水大队");
                this.SCXDDropDownList.DataBind();
                ////获取“第一注水小队”下的注水井列表
                //this.JHDropDownList.DataSource = GetZSJData("第一注水小队");
                //this.JHDropDownList.DataBind();
                this.productdate_box.Value = DateTime.Now.ToString("yyyy-MM-dd");
                DataTable dt = GetData();
                ReportDataSource rds = new ReportDataSource("ZSJReportDataSet", dt);
                this.ReportViewer1.LocalReport.ReportPath = Server.MapPath("..\\Reports\\ZSJReport.rdlc");
                ReportViewer1.KeepSessionAlive = true;//解决ASP.NET session has expired or could not be found问题
                ReportViewer1.LocalReport.DataSources.Clear();
                ReportViewer1.LocalReport.DataSources.Add(rds);
                ReportViewer1.LocalReport.Refresh();
            }
        }

        //获取所有工作区的名称
        private List<string> GetWorkZoneData()
        {
            List<string> WorkZone = new List<string>();
            string strSqlStr = "SELECT [Dep_Name] FROM [dbo].[Tb_Department] WHERE [ParentDep_ID]='ALL'";
            framework.SqlHelperCS.SqlHelper cs = new framework.SqlHelperCS.SqlHelper();
            DataSet DSet = cs.GetDataSet(strSqlStr);
            if (DSet != null)
            {
                DataTable DTable = DSet.Tables[0];
                for (int i = 0; i < DTable.Rows.Count; i++)
                {
                    DataRowView rowview = DSet.Tables[0].DefaultView[i];
                    WorkZone.Add(rowview["Dep_Name"].ToString());
                }
            }
            return WorkZone;
        }

        //根据生产小队名称得到注水井列表
        private List<string> GetZSJData(string strSCXD)
        {
            List<string> CYJJList = new List<string>();
            string strSqlStr = "SELECT [Well_ID] FROM [dbo].[Tb_CommonWell]  WHERE [Dep_ID] IN (SELECT [Dep_ID] FROM [dbo].[Tb_Department] WHERE [Dep_Name] = '" + strSCXD + "') AND SUBSTRING([Well_ID],0,3)='ZS'";
            framework.SqlHelperCS.SqlHelper cs = new framework.SqlHelperCS.SqlHelper();
            DataSet DSet = cs.GetDataSet(strSqlStr);
            if (DSet != null)
            {
                DataTable DTable = DSet.Tables[0];
                for (int i = 0; i < DTable.Rows.Count; i++)
                {
                    DataRowView rowview = DSet.Tables[0].DefaultView[i];
                    CYJJList.Add(rowview["Well_ID"].ToString());
                }
            }
            return CYJJList;
        }

        //根据工作区名称得到注水大队列表
        private List<string> GetZSDDData(string zone)
        {
            List<string> ZSDDList = new List<string>();
            string strSqlStr = "SELECT [Dep_Name] FROM [dbo].[Tb_Department] WHERE [ParentDep_ID] IN (SELECT [Dep_ID] FROM [dbo].[Tb_Department] WHERE [Dep_Name] = '" + zone + "') AND SUBSTRING([dbo].[Tb_Department].[Dep_ID],0,3)='ZS'";
            framework.SqlHelperCS.SqlHelper cs = new framework.SqlHelperCS.SqlHelper();
            DataSet DSet = cs.GetDataSet(strSqlStr);
            if (DSet != null)
            {
                DataTable DTable = DSet.Tables[0];
                for (int i = 0; i < DTable.Rows.Count; i++)
                {
                    DataRowView rowview = DSet.Tables[0].DefaultView[i];
                    ZSDDList.Add(rowview["Dep_Name"].ToString());
                }
            }
            return ZSDDList;
        }

        private DataTable GetData()
        {
            List<string> Well_ID = new List<string>(), Well_Section = new List<string>(), Date = new List<string>(), Daily_Water_Time = new List<string>(), Daily_Schedule = new List<string>(), Daily_Actual = new List<string>(), Oil_Pressure = new List<string>(), Casing_Pressure = new List<string>(), Pump_Pressure = new List<string>(), Comment = new List<string>();
            string productdate = this.productdate_box.Value;
            DateTime dt_pd = Convert.ToDateTime(productdate);
            string workZone = this.WorkZoneDropDownList.SelectedValue;
            string SCDD = this.SCDDDropDownList.SelectedValue;
            //string ZSJ = this.JHDropDownList.SelectedValue;
            string strSqlStr = "SELECT  * FROM [dbo].[Tb_Oil_Well]";
            DataTable dt = new DataTable("ZSJDataTable");//这个名字是无关紧要的，xsd的DataTable也可以随意命名
            //if (JHDropDownList.SelectedValue == "--全部--")//把该注水小队下的注水井全部选择
            {
                strSqlStr = "SELECT  * FROM  [dbo].[Tb_Water_Injection_Well] WHERE   Date = '" + dt_pd + "' AND SUBSTRING([Well_ID], 0, 4) = 'ZSJ' AND SUBSTRING([Well_ID], LEN([Well_ID]) - 6, 1) = '1' AND SUBSTRING([Well_ID], LEN([Well_ID]) - 4, 1) = '1' AND SUBSTRING([Well_ID], LEN([Well_ID]) - 2, 1) = '1'";
            }
            //else
            //{
            //    strSqlStr = "SELECT  * FROM [dbo].[Tb_Water_Injection_Well] WHERE Date = '" + dt_pd + "' AND [Well_ID]='" + ZSJ + "'";
            //}
            framework.SqlHelperCS.SqlHelper cs = new framework.SqlHelperCS.SqlHelper();
            DataSet DSet = cs.GetDataSet(strSqlStr);
            if (DSet != null)
            {
                DataTable DTable = DSet.Tables[0];
                for (int i = 0; i < DTable.Rows.Count; i++)
                {
                    DataRowView rowview = DSet.Tables[0].DefaultView[i];
                    Well_ID.Add(rowview["Well_ID"].ToString());
                    Well_Section.Add(rowview["Well_Section"].ToString());
                    Date.Add(rowview["Date"].ToString());
                    Daily_Water_Time.Add(rowview["Daily_Water_Time"].ToString());
                    Daily_Schedule.Add(rowview["Daily_Schedule"].ToString());
                    Daily_Actual.Add(rowview["Daily_Actual"].ToString());
                    Oil_Pressure.Add(rowview["Oil_Pressure"].ToString());
                    Casing_Pressure.Add(rowview["Casing_Pressure"].ToString());
                    Pump_Pressure.Add(rowview["Pump_Pressure"].ToString());
                    Comment.Add(rowview["Comment"].ToString());
                }
                dt.Columns.Add(new DataColumn("Well_ID", typeof(string)));
                dt.Columns.Add(new DataColumn("Well_Section", typeof(string)));
                dt.Columns.Add(new DataColumn("Date", typeof(string)));
                dt.Columns.Add(new DataColumn("Daily_Water_Time", typeof(string)));
                dt.Columns.Add(new DataColumn("Daily_Schedule", typeof(string)));
                dt.Columns.Add(new DataColumn("Daily_Actual", typeof(string)));
                dt.Columns.Add(new DataColumn("Oil_Pressure", typeof(string)));
                dt.Columns.Add(new DataColumn("Casing_Pressure", typeof(string)));
                dt.Columns.Add(new DataColumn("Pump_Pressure", typeof(string)));
                dt.Columns.Add(new DataColumn("Comment", typeof(string)));
                for (int i = 0; Well_ID.Count > 0 && i < Well_ID.Count; i++)
                {
                    DataRow row = dt.NewRow();
                    row["Well_ID"] = Well_ID[i];
                    row["Well_Section"] = Well_Section[i];
                    row["Date"] = Date[i];
                    row["Daily_Water_Time"] = Daily_Water_Time[i];
                    row["Daily_Schedule"] = Daily_Schedule[i];
                    row["Daily_Actual"] = Daily_Actual[i];
                    row["Oil_Pressure"] = Oil_Pressure[i];
                    row["Casing_Pressure"] = Casing_Pressure[i];
                    row["Pump_Pressure"] = Pump_Pressure[i];
                    row["Comment"] = Comment[i];
                    dt.Rows.Add(row);
                }
                return dt;
            }
            return null;
        }

        //“工作区”选项变换时，“生产大队”的选项也要改变
        protected void WorkZoneDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.SCDDDropDownList.DataSource = GetZSDDData(WorkZoneDropDownList.SelectedItem.Text);
            this.SCDDDropDownList.DataBind();
        }

        protected void SCDDDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            //this.JHDropDownList.DataSource = GetZSJData(SCDDDropDownList.SelectedItem.Text);
            //this.JHDropDownList.DataBind();
        }

        protected void queryBtn_Click(object sender, EventArgs e)
        {
            DataTable dt = GetData();
            ReportDataSource rds = new ReportDataSource("ZSJReportDataSet", dt);
            this.ReportViewer1.LocalReport.ReportPath = Server.MapPath("..\\Reports\\ZSJReport.rdlc");
            ReportViewer1.LocalReport.DataSources.Clear();
            ReportViewer1.LocalReport.DataSources.Add(rds);
            ReportViewer1.LocalReport.Refresh();
        }

        //“生产小队”选项变换时，“注水井井号”的选项也要改变
        protected void SCXDDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            //JHDropDownList.DataSource = GetZSJData(SCXDDropDownList.SelectedItem.Text);
            //JHDropDownList.DataBind();
        }
    }
}